plot_all <- function(abs = T,
                     years = c(1900, 2020), 
                     data.df = episode_data_wo_CI) {
  
  eps <- data.df
  
  stopifnot(is.logical(abs), length(abs) == 1)
  
  stopifnot(is.numeric(years), length(years) == 2, years[2] > years[1])
  
  #perhaps this is redundant 
  if(min(years)<min(eps$year) | max(years)>max(eps$year))
    stop("Error: Data not available for time range")
  
  if (isTRUE(abs)) {
    eps_year <- eps %>%
      dplyr::filter(between(year, min(years), max(years))) %>%
      {if(nrow(.) == 0) stop("No episodes during selected time period. No plot generated") else .} %>% 
      dplyr::group_by(year) %>%
      dplyr::summarise(increase_episodes = sum(increase_episode),
                       decline_episodes = sum(decline_episode)) %>%
      tidyr::pivot_longer(cols = c(increase_episodes, decline_episodes), names_to = "ep_type", values_to = "countries")
    
  } else {
    eps_year <- eps %>%
      dplyr::filter(between(year, min(years), max(years))) %>%
      dplyr::group_by(year) %>%
      dplyr::summarise(increase_episodes = sum(increase_episode) / length(unique(country_id)),
                       decline_episodes = sum(decline_episode) / length(unique(country_id))) %>%
      tidyr::pivot_longer(cols = c(increase_episodes, decline_episodes), names_to = "ep_type", values_to = "countries")
  }
  
  p <-  ggplot2::ggplot(data = eps_year, aes(x = year, y = countries, group = ep_type, linetype = ep_type)) +
    geom_line() +
    scale_x_continuous(breaks = seq(round(min(years) / 10) * 10, round(max(years) / 10) * 10, 10)) +
    scale_linetype(name = "", breaks = c("increase_episodes", "decline_episodes"), labels = c("Growth Episodes", "Decline Episodes")) +
    xlab("Year") +
    theme_classic() +
    theme(legend.position = "bottom")
  
  if (isTRUE(abs)) {
    p +  ylab("Number of Countries")
  }  else {
    p +  ylab("Countries (%)")
  }
}